<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\index\controller;

use think\Controller;

/**
 * 订单控制器
 * Class Index
 * @package app\index\controller
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/04/05 10:38
 */
class Order extends Controller
{

    /**
     * 订单列表
     */
    public function index()
    {
        $this->redirect('@admin');
    }

    public function qrc()
    {
        $wechat = load_wechat('Extends');
        for ($i = 10; $i < 90; $i++) {
            $qrc = $wechat->getQRCode($i, 1);
            print_r($qrc);
        }

    }

    //生成订单
    public function save()
    {
        $data = $_POST;
        if($data['video_id']){

            $detail = Db::table('Videos')->find('video_id');
            if ( $detail ) {
                if ( $detail['price'] != $data['price'] )
                {
                    $data['code'] ='400';
                    $data['message'] = '该课程不存在';
                    return json_encode($data);
                } else {
                    //
                    $order = array();
                    $rand = rand(1000,9999);
                    $order['ordernum'] = 'V'.time().$rand;
                    $order['create_time'] = time();
                    $order['user_id'] = session('user_id');
                    $order['price'] = $data['price'];
                    $order['status'] = 0;
                    $flag = Db::table('Orders')->insertGetid($order);
                    if ( $flag ) {
                        //
                        $this->redirect('WeixinPay/pay/order_id/'.$flag); //跳转支付
                    } else {
                        $data['code'] ='400';
                        $data['message'] = '生成订单失败，请稍后重试';
                        return json_encode($data);
                    }

                }

            } else {
                $data['code'] ='400';
                $data['message'] = '该课程不存在';
                return json_encode($data);
            }
        } else {
            $data['code'] ='400';
            $data['message'] = '参数错误';
            return json_encode($data);
        }
    }


    //
    public function check($video_id)
    {
        if ( $video_id ) {
            $flag = Db::table('Videos')->where(['isdelete'=>0,'id'=>$video_id])->find();
            if ( $flag ){
                return true;
            }
        } else {
            return false;
        }
    }


}
